package com.android.kotlincoroutinemvvmdemo.dialog
import android.app.Dialog
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.android.kotlincoroutinemvvmdemo.Constants
import com.android.kotlincoroutinemvvmdemo.CoroutineMVVMApp

abstract class BaseDialog<T : ViewDataBinding>(
    context: Context,
    theme: Int = 0
) : Dialog(context, theme) {

    protected val mContext = context

    protected lateinit var mBinding : T

    private var mReceiver : AdapterUIModeNightBroadcastReceiver? = null

    private val mIntentFilter by lazy {
        IntentFilter().apply {
            addAction(Constants.DAY_NIGHT_UI_MODE_CHANGE)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window?.apply {
            decorView.background = ColorDrawable(0x00000000)
            val mAttribute = attributes
            mAttribute.gravity = Gravity.CENTER
            //通过WindowManager.LayoutParams设置宽高
            //mAttribute.width = WindowManager.LayoutParams.WRAP_CONTENT
            //mAttribute.height = WindowManager.LayoutParams.WRAP_CONTENT

            //通过设置type类型值，使得弹窗可以在不同进程的UI上显示，需要添加android.permission.SYSTEM_ALERT_WINDOW权限
            mAttribute.type =
                //if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
                /*else*/ WindowManager.LayoutParams.TYPE_APPLICATION

            attributes = mAttribute
        }
        mBinding =
            DataBindingUtil.inflate(LayoutInflater.from(mContext), getLayoutId(), null, false)
        setContentView(mBinding.root)
    }

    override fun onStart() {
        super.onStart()
        mReceiver = AdapterUIModeNightBroadcastReceiver()
        mContext.registerReceiver(mReceiver, mIntentFilter)
        adapterUIModeNightForDialog(CoroutineMVVMApp.instance!!.isDarkTheme())
    }

    override fun onStop() {
        super.onStop()
        mContext.unregisterReceiver(mReceiver)
        mReceiver = null
    }

    abstract fun getLayoutId(): Int

    open fun adapterUIModeNightForDialog(uiModeNight: Boolean) {}

    inner class AdapterUIModeNightBroadcastReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (context == null || intent == null) {
                return
            }
            when (intent.action) {
                Constants.DAY_NIGHT_UI_MODE_CHANGE -> {
                    val dayNightYes = intent.getBooleanExtra(Constants.DAY_NIGHT_FLAG, false)
                    adapterUIModeNightForDialog(dayNightYes)
                }
            }
        }
    }

}